package pe.com.certicom.ittsa.control.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import pe.com.certicom.ittsa.control.form.DispAsientoForm;
import pe.com.certicom.ittsa.model.bean.BeanGeoCode;
import pe.com.certicom.ittsa.model.bean.ReporteDispAsientoBean;
import pe.com.certicom.ittsa.model.bo.DispAsientoBO;
import pe.com.certicom.ittsa.model.bean.ClienteBean;
import pe.com.certicom.ittsa.model.constants.ValueConstants;
import pe.com.certicom.ittsa.framework.exception.DaoException;

public class DispAsientoAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward actionforward = null;
		HttpSession session = request.getSession(true);
		int method = Integer.parseInt(request.getParameter("method"));

		try {
			switch (method) {
			case 10:
				actionforward = inicio(mapping, form, request, response,
						session);
				break;
			case 11:
				actionforward = irMenuDisponibilidadAsiento(mapping, form,
						request, response, session);
				break;
			case 12:
				actionforward = irCargarDestino(mapping, form, request,
						response, session);
				break;
			case 13:
				actionforward = irMostrarReporteDispAsiento(mapping, form,
						request, response, session);
				break;
			case 20:
				actionforward = irRegistrarCliente(mapping, form, request,
						response, session);
				break;
			case 50:
				actionforward = irMenuCompraPasajes(mapping, form, request,
						response, session);
				break;
			}

		} catch (Exception e) {
			e.getMessage();
			System.out.println("Error en Switch Case ... " + e);
		}
		return actionforward;
	}

	public ActionForward inicio(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			HttpSession session) throws IOException, ServletException,
			DaoException {
		DispAsientoForm formulario = (DispAsientoForm) form;

		return mapping.findForward(ValueConstants.MENU_INICIO);
	}

	public ActionForward irMenuDisponibilidadAsiento(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response, HttpSession session)
			throws IOException, ServletException, Exception {
		DispAsientoForm formulario = (DispAsientoForm) form;
		// UsuarioBean usuario = (UsuarioBean)
		// session.getAttribute(ValueConstants.USUARIO_SESSION);
		// log.info(ValueConstants.USUARIO + usuario.getLogUsuario());
		Map map = new HashMap();
		List listaOrigen = new ArrayList();
		List listaDestino = new ArrayList();
		listaOrigen = DispAsientoBO.obtenerInstancia().listarOrigen();

		map.put(ValueConstants.LISTA_ORIGEN, listaOrigen);
		map.put(ValueConstants.LISTA_DESTINO, listaDestino);

		formulario.setMapOrigen(map);
		if(formulario.getMapDestino()==null){
		formulario.setMapDestino(map);
		}
		//formulario.setDepartamento(new BeanGeoCode());
		return mapping.findForward(ValueConstants.MENU_DISP_ASIENTO);

	}

	public ActionForward irCargarDestino(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response, HttpSession session)
			throws IOException, ServletException, Exception {
		DispAsientoForm formulario = (DispAsientoForm) form;
		BeanGeoCode departamento = formulario.getDepartamento();
		// UsuarioBean usuario = (UsuarioBean)
		// session.getAttribute(ValueConstants.USUARIO_SESSION);
		// log.info(ValueConstants.USUARIO + usuario.getLogUsuario());
		Map map = new HashMap();
		List listaDestino = new ArrayList();
		listaDestino = DispAsientoBO.obtenerInstancia().listarDestino(
				departamento.getIdDepartamento());
		map.put(ValueConstants.LISTA_DESTINO, listaDestino);
		formulario.setMapDestino(map);
		
		if( formulario.getDepartamento().getFecha()!=null && !formulario.getDepartamento().getFecha().equals("")) {
			request.setAttribute("fecha",formulario.getDepartamento().getFecha().trim());
		}

		return mapping.findForward(ValueConstants.MENU_DISP_ASIENTO);
	}

	public ActionForward irRegistrarCliente(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response, HttpSession session)
			throws IOException, ServletException, DaoException {
		DispAsientoForm formulario = (DispAsientoForm) form;
		List<ClienteBean> listaClientes = new ArrayList<ClienteBean>();
		formulario.setClienteBean(new ClienteBean());
		formulario.setListaClientes(listaClientes);
		return mapping.findForward(ValueConstants.REGISTRO_CLIENTE);
	}

	public ActionForward irMostrarReporteDispAsiento(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response, HttpSession session)
			throws IOException, ServletException, Exception {
		DispAsientoForm formulario = (DispAsientoForm) form;
		BeanGeoCode departamento = formulario.getDepartamento();

		ArrayList lugares = new ArrayList();
		lugares = (ArrayList) DispAsientoBO.obtenerInstancia().listarOrigen();
		for (Object a : lugares) {
			if (departamento.getIdDepartamento() == ((BeanGeoCode) (a))
					.getIdDepartamento()) {
				departamento.setDepartamentoOrigen(((BeanGeoCode) (a))
						.getDepartamento());
				break;
			}
		}
		List listaReporte = new ArrayList();
		listaReporte = DispAsientoBO.obtenerInstancia().listarReporte(
				departamento);
		if(listaReporte.size()<1){
			request.setAttribute("alerta", "No se encontro Informacion");
		return mapping.findForward(ValueConstants.MENU_DISP_ASIENTO);	
		}

		for (Object a : listaReporte) {
			((ReporteDispAsientoBean) (a)).setDestino(formulario
					.getDepartamento().getDepartamento());
			System.out.println(((ReporteDispAsientoBean) (a)).getCosto1ro());
			System.out.println(((ReporteDispAsientoBean) (a)).getCosto2do());
			System.out.println(((ReporteDispAsientoBean) (a)).getDestino());
			System.out.println(((ReporteDispAsientoBean) (a)).getDisponibles());
			System.out.println(((ReporteDispAsientoBean) (a)).getHora());
			System.out.println(((ReporteDispAsientoBean) (a)).getOrigen());
			System.out.println(((ReporteDispAsientoBean) (a)).getServicio());
		}
		formulario.setListaReporteDispAsiento(listaReporte);
		return mapping.findForward(ValueConstants.MENU_REPORTE_DISP_ASIENTO);
	}

	public ActionForward irMenuCompraPasajes(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response, HttpSession session)
			throws IOException, ServletException, Exception {
		DispAsientoForm formulario = (DispAsientoForm) form;
		Map map = new HashMap();
		List listaOrigen = new ArrayList();
		List listaDestino = new ArrayList();
		listaOrigen = DispAsientoBO.obtenerInstancia().listarOrigen();

		map.put(ValueConstants.LISTA_ORIGEN, listaOrigen);
		map.put(ValueConstants.LISTA_DESTINO, listaDestino);

		formulario.setMapOrigen(map);
		formulario.setMapDestino(map);

		return mapping.findForward(ValueConstants.MENU_DISP_ASIENTO);

	}
}
